package com.maiji.cloud.microservice;

import com.google.common.collect.Lists;
import com.maiji.cloud.config.MyRedisConfig;
import com.maiji.cloud.entities.login.AppUser;
import com.maiji.cloud.entities.shopingmall.CapitalMain;
import com.maiji.cloud.request.shopingmall.DistributeFundReqDto;
import com.maiji.cloud.request.shopingmall.DistributeIntegralReqDto;
import com.maiji.cloud.response.BaseDataResDto;
import com.maiji.cloud.response.shopingmall.FinancialDetailsResData;
import com.maiji.cloud.service.CapitalMainService;
import com.maiji.cloud.utils.FlowTypeEnum;
import com.maiji.cloud.utils.RedisUtil;
import com.maiji.cloud.utils.UUID_MD5;
import org.eclipse.jetty.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/capitalMain/")
public class CapitalMainController {

    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
	@Autowired
	private CapitalMainService capitalMainService;

	/**
	 * 分发减肥基金
	 * 
	 * @param distributeFundReqDto
	 * @param maijiToken
	 * @return
	 */
	@PostMapping("distributeFundByToken")
	public Boolean distributeFundByToken(@RequestBody DistributeFundReqDto distributeFundReqDto,
			@RequestHeader(name = "maijiToken") String maijiToken) {
		Double fund = distributeFundReqDto.getData().getFund();
        FlowTypeEnum remark = distributeFundReqDto.getData().getRemark();
		String relevantId = distributeFundReqDto.getData().getRelevantId();
		if (fund == null || fund.doubleValue() == 0 || remark == null) return false;
        AppUser appUser = RedisUtil.getAppUserByRedis(redisTemplate, MyRedisConfig.REDISDATABASENO, maijiToken, userInfoService);
		return capitalMainService.updateCapitalMain(appUser.getUuId(), FlowTypeEnum.FUND, fund.toString(), FlowTypeEnum.INCREASE, remark, relevantId);
	}

	/**
	 * 分发积分
	 * 
	 * @param distributeIntegralReqDto
	 * @param maijiToken
	 * @return
	 */
	@PostMapping("distributeIntegralByToken")
	public Boolean distributeIntegralByToken(@RequestBody DistributeIntegralReqDto distributeIntegralReqDto,
			@RequestHeader(name = "maijiToken") String maijiToken) {
		Integer integral = distributeIntegralReqDto.getData().getIntegral();
        FlowTypeEnum remark = distributeIntegralReqDto.getData().getRemark();
		String relevantId = distributeIntegralReqDto.getData().getRelevantId();
		if (integral == null || integral.intValue() == 0 || remark == null) return false;
        AppUser appUser = RedisUtil.getAppUserByRedis(redisTemplate, MyRedisConfig.REDISDATABASENO, maijiToken, userInfoService);
		return capitalMainService.updateCapitalMain(appUser.getUuId(), FlowTypeEnum.INTEGRAL, integral.toString(), FlowTypeEnum.INCREASE, remark, relevantId);
	}

	/**
	 * 通过remarkInt 分发积分 现用于 社区：发布 点赞 转发 分享
	 * @param distributeIntegralReqDto
	 * @param maijiToken
	 * @return
	 */
	@PostMapping("distributeIntegralByRemarkInt")
	public BaseDataResDto<FinancialDetailsResData> distributeIntegralByRemarkInt(@RequestBody DistributeIntegralReqDto distributeIntegralReqDto,@RequestHeader(name = "maijiToken") String maijiToken){
		return capitalMainService.distributeIntegralByRemarkInt(distributeIntegralReqDto,maijiToken);
	}

    /**
     * 创建用户资金账户
     * @param userId
     * @return
     */
    @PostMapping("createCapitalMain")
    public Boolean createCapitalMain (@RequestBody String userId) {
        if (StringUtil.isBlank(userId)) return false;
        return capitalMainService.createCapitalMain(userId);
    }

    /**
     * 获取用户账户资金信息列表
     * @param userIds
     * @return
     */
    @PostMapping("findAllCapitalMains")
    public List<CapitalMain> findAllCapitalMains (@RequestBody List<String> userIds) {
        if (userIds.size() == 0) return Lists.newArrayList();
        return capitalMainService.findAllCapitalMains(userIds);
    }

    /**
     * 获取用户资金统计数据
     * @return
     */
    @PostMapping("findCapitalMainCount")
    public BaseDataResDto<Map<String, Object>> findCapitalMainCount () {
        return capitalMainService.findCapitalMainCount();
    }
	
}
